package extensions;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import play.templates.JavaExtensions;

import com.ineatconseil.yougo.dto.RequestDto;

public class YougoUtilsExtensions extends JavaExtensions {

	public static Calendar addDay(Calendar date) {
		date.add(Calendar.DAY_OF_YEAR, 1);
		return date;
	}

	public static Collection<Month> getMonths(Calendar date, int days) {
		Map<Integer, Month> months = new HashMap<Integer, Month>();
		Calendar clone = (Calendar) date.clone();
		for (int i = 0; i <= days; i++) {
			clone.add(Calendar.DAY_OF_YEAR, 1);
			int month = clone.get(Calendar.MONTH);
			if (months.containsKey(month)) {
				months.get(month).days++;
			} else {
				months.put(month, new Month(clone.getTime(), 1));
			}
		}
		return months.values();
	}

	public static RequestDto getRequest(Calendar cal, Set<RequestDto> requests) {
		for (RequestDto req : requests) {
			if (req.getFrom().equals(cal.getTime())) {
				return req;
			} else if (req.getTo().equals(cal.getTime())) {
				return req;
			} else if (req.getFrom().before(cal.getTime())
					&& req.getTo().after(cal.getTime())) {
				return req;
			}
		}
		return null;
	}

	public static int getNbDays(Date from, Date to) {
		return (int) ((to.getTime() - from.getTime()) / (1000 * 60 * 60 * 24));

	}

}
